package net.bwie.realtime.jtp.dwd.log.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.bwie.realtime.jtp.dwd.log.function.AdjustIsNewProcessFunctionlx;
import net.bwie.realtime.jtp.dwd.log.function.AppLogSplitProcessFunctionlx;
import net.bwie.realtime.jtp.utils.KafkaUtil;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SideOutputDataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class JtpAppLogEtljoblx {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStream<String> kafkadataStream = KafkaUtil.consumerKafka(env, "topic-log");

        DataStream<String> pageStream = processLog(kafkadataStream);
        pageStream.print("pageStream");

        KafkaUtil.producerKafka(pageStream,"dwd-traffic-page-log");

        env.execute("JtpAppLogEtljoblx");
    }

    private static DataStream<String> processLog(DataStream<String> stream) {

        DataStream<String> jsonStream = appLogCleaned(stream);

        DataStream<String> etlStream = processIsNew(jsonStream);

        DataStream<String> pageStream = splitStream(etlStream);
        return pageStream;
    }

    private static DataStream<String> splitStream(DataStream<String> stream) {
        final  OutputTag<String> startTag = new OutputTag<String>("start-log") {
        };
        final  OutputTag<String> errorTag = new OutputTag<String>("error-log") {
        };
        final  OutputTag<String> displayTag = new OutputTag<String>("display-log") {
        };
        final OutputTag<String> actionTag = new OutputTag<String>("action-log") {
        };
        SingleOutputStreamOperator<String> process =
                stream.process(new AppLogSplitProcessFunctionlx(startTag, errorTag, displayTag, actionTag));
        return process;
    }

    private static DataStream<String> processIsNew(DataStream<String> stream) {
        KeyedStream<String, String> midStream = stream.keyBy(new KeySelector<String, String>() {
            @Override
            public String getKey(String s) throws Exception {
                JSONObject jsonObject = JSON.parseObject(s);

                return jsonObject.getJSONObject("common").getString("mid");
            }
        });
        SingleOutputStreamOperator<String> isNewProcess = midStream.process(new AdjustIsNewProcessFunctionlx());
        return isNewProcess;
    }

    private static DataStream<String> appLogCleaned(DataStream<String> stream) {
        OutputTag<String> dirtyTag = new OutputTag<String>("dirty-log") {
        };
        SingleOutputStreamOperator<String> cleanedStream = stream.process(new ProcessFunction<String, String>() {
            @Override
            public void processElement(String s, ProcessFunction<String, String>.Context context, Collector<String> collector) throws Exception {
                try {
                    JSON.parseObject(s);
                    collector.collect(s);
                } catch (Exception e) {
                    context.output(dirtyTag, s);
                }
            }
        });
        SideOutputDataStream<String> dirtyStream = cleanedStream.getSideOutput(dirtyTag);
        KafkaUtil.producerKafka(dirtyStream,"dwd-traffic-dirty-log");
        return cleanedStream;
    }
}
